{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<script type=\"text/javascript\">window.PlotlyConfig = {MathJaxConfig: 'local'};</script><script type=\"text/javascript\">if (window.MathJax) {MathJax.Hub.Config({SVG: {font: \"STIX-Web\"}});}</script><script>requirejs.config({paths: { 'plotly': ['https://cdn.plot.ly/plotly-latest.min']},});if(!window._Plotly) {require(['plotly'],function(plotly) {window._Plotly=plotly;});}</script>"
      ],
      "text/vnd.plotly.v1+html": [
       "<script type=\"text/javascript\">window.PlotlyConfig = {MathJaxConfig: 'local'};</script><script type=\"text/javascript\">if (window.MathJax) {MathJax.Hub.Config({SVG: {font: \"STIX-Web\"}});}</script><script>requirejs.config({paths: { 'plotly': ['https://cdn.plot.ly/plotly-latest.min']},});if(!window._Plotly) {require(['plotly'],function(plotly) {window._Plotly=plotly;});}</script>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import plotly\n",
    "plotly.offline.init_notebook_mode(connected=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Basic Sankey Chart"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.plotly.v1+json": {
       "config": {
        "linkText": "Export to plot.ly",
        "plotlyServerURL": "https://plot.ly",
        "showLink": true
       },
       "data": [
        {
         "link": {
          "source": [
           0,
           0,
           1,
           2,
           3,
           3
          ],
          "target": [
           2,
           3,
           3,
           4,
           4,
           5
          ],
          "value": [
           8,
           4,
           2,
           8,
           4,
           2
          ]
         },
         "node": {
          "color": [
           "#e03636",
           "#edd08e",
           "#ff534d",
           "#23d58e",
           "#25c6fc",
           "#0000ff"
          ],
          "label": [
           "A1",
           "A2",
           "B1",
           "B2",
           "C1",
           "C2"
          ],
          "line": {
           "color": "black",
           "width": 1
          },
          "thickness": 20
         },
         "type": "sankey"
        }
       ],
       "layout": {
        "font": {
         "size": 20
        },
        "title": "基础桑吉图"
       }
      },
      "text/html": [
       "<div id=\"d4b51bcc-05a3-4592-a77a-7a36edfdf603\" style=\"height: 525px; width: 100%;\" class=\"plotly-graph-div\"></div><script type=\"text/javascript\">require([\"plotly\"], function(Plotly) { window.PLOTLYENV=window.PLOTLYENV || {};window.PLOTLYENV.BASE_URL=\"https://plot.ly\";Plotly.newPlot(\"d4b51bcc-05a3-4592-a77a-7a36edfdf603\", [{\"type\": \"sankey\", \"node\": {\"thickness\": 20, \"line\": {\"color\": \"black\", \"width\": 1}, \"label\": [\"A1\", \"A2\", \"B1\", \"B2\", \"C1\", \"C2\"], \"color\": [\"#e03636\", \"#edd08e\", \"#ff534d\", \"#23d58e\", \"#25c6fc\", \"#0000ff\"]}, \"link\": {\"source\": [0, 0, 1, 2, 3, 3], \"target\": [2, 3, 3, 4, 4, 5], \"value\": [8, 4, 2, 8, 4, 2]}}], {\"title\": \"\\u57fa\\u7840\\u6851\\u5409\\u56fe\", \"font\": {\"size\": 20}}, {\"showLink\": true, \"linkText\": \"Export to plot.ly\", \"plotlyServerURL\": \"https://plot.ly\"})});</script><script type=\"text/javascript\">window.addEventListener(\"resize\", function(){window._Plotly.Plots.resize(document.getElementById(\"d4b51bcc-05a3-4592-a77a-7a36edfdf603\"));});</script>"
      ],
      "text/vnd.plotly.v1+html": [
       "<div id=\"d4b51bcc-05a3-4592-a77a-7a36edfdf603\" style=\"height: 525px; width: 100%;\" class=\"plotly-graph-div\"></div><script type=\"text/javascript\">require([\"plotly\"], function(Plotly) { window.PLOTLYENV=window.PLOTLYENV || {};window.PLOTLYENV.BASE_URL=\"https://plot.ly\";Plotly.newPlot(\"d4b51bcc-05a3-4592-a77a-7a36edfdf603\", [{\"type\": \"sankey\", \"node\": {\"thickness\": 20, \"line\": {\"color\": \"black\", \"width\": 1}, \"label\": [\"A1\", \"A2\", \"B1\", \"B2\", \"C1\", \"C2\"], \"color\": [\"#e03636\", \"#edd08e\", \"#ff534d\", \"#23d58e\", \"#25c6fc\", \"#0000ff\"]}, \"link\": {\"source\": [0, 0, 1, 2, 3, 3], \"target\": [2, 3, 3, 4, 4, 5], \"value\": [8, 4, 2, 8, 4, 2]}}], {\"title\": \"\\u57fa\\u7840\\u6851\\u5409\\u56fe\", \"font\": {\"size\": 20}}, {\"showLink\": true, \"linkText\": \"Export to plot.ly\", \"plotlyServerURL\": \"https://plot.ly\"})});</script><script type=\"text/javascript\">window.addEventListener(\"resize\", function(){window._Plotly.Plots.resize(document.getElementById(\"d4b51bcc-05a3-4592-a77a-7a36edfdf603\"));});</script>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "data = dict(\n",
    "    type = 'sankey',\n",
    "    # 设置每个柱状条的颜色，宽度，边框等信息\n",
    "    node = dict(\n",
    "      thickness = 20,\n",
    "      line = dict(color = \"black\", width = 1),\n",
    "      label = [\"A1\", \"A2\", \"B1\", \"B2\", \"C1\", \"C2\"],\n",
    "      color = [\"#e03636\", \"#edd08e\", \"#ff534d\", \"#23d58e\", \"#25c6fc\", \"#0000ff\"]\n",
    "    ),\n",
    "    # 设置能量流动条的宽度值\n",
    "    link = dict(\n",
    "      source = [0, 0, 1, 2, 3, 3],\n",
    "      target = [2, 3, 3, 4, 4, 5],\n",
    "      value = [8, 4, 2, 8, 4, 2]\n",
    "  )\n",
    ")\n",
    "\n",
    "layout =  dict(\n",
    "    title = \"基础桑吉图\",\n",
    "    font = dict(size = 20)\n",
    ")\n",
    "fig = dict(data=[data], layout=layout)\n",
    "plotly.offline.iplot(fig, validate=False)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Advance Sankey Chart"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.plotly.v1+json": {
       "config": {
        "linkText": "Export to plot.ly",
        "plotlyServerURL": "https://plot.ly",
        "showLink": true
       },
       "data": [
        {
         "link": {
          "source": [
           0,
           0,
           1,
           1,
           2,
           2,
           3,
           3,
           4,
           4,
           6,
           6,
           6,
           6,
           5,
           5,
           5,
           5
          ],
          "target": [
           5,
           6,
           5,
           6,
           5,
           6,
           5,
           6,
           5,
           6,
           7,
           8,
           9,
           10,
           7,
           8,
           9,
           10
          ],
          "value": [
           5,
           25,
           6,
           25,
           4,
           40,
           7,
           42,
           6,
           40,
           62,
           37,
           70,
           3,
           5,
           15,
           7,
           1
          ]
         },
         "node": {
          "label": [
           "专业A",
           "专业B",
           "专业C",
           "专业D",
           "专业E",
           "转到其他学院",
           "留在本学院",
           "国内升学",
           "国外深造",
           "直接就业",
           "待就业"
          ],
          "line": {
           "color": "black",
           "width": 0.5
          },
          "pad": 15,
          "thickness": 15
         },
         "type": "sankey"
        }
       ],
       "layout": {
        "font": {
         "color": "black",
         "size": 15
        },
        "title": "学院学生转专业统计 & 毕业去向统计"
       }
      },
      "text/html": [
       "<div id=\"0bf26a82-2a42-4ab7-b4de-d3dbec0986dd\" style=\"height: 525px; width: 100%;\" class=\"plotly-graph-div\"></div><script type=\"text/javascript\">require([\"plotly\"], function(Plotly) { window.PLOTLYENV=window.PLOTLYENV || {};window.PLOTLYENV.BASE_URL=\"https://plot.ly\";Plotly.newPlot(\"0bf26a82-2a42-4ab7-b4de-d3dbec0986dd\", [{\"type\": \"sankey\", \"node\": {\"pad\": 15, \"thickness\": 15, \"line\": {\"color\": \"black\", \"width\": 0.5}, \"label\": [\"\\u4e13\\u4e1aA\", \"\\u4e13\\u4e1aB\", \"\\u4e13\\u4e1aC\", \"\\u4e13\\u4e1aD\", \"\\u4e13\\u4e1aE\", \"\\u8f6c\\u5230\\u5176\\u4ed6\\u5b66\\u9662\", \"\\u7559\\u5728\\u672c\\u5b66\\u9662\", \"\\u56fd\\u5185\\u5347\\u5b66\", \"\\u56fd\\u5916\\u6df1\\u9020\", \"\\u76f4\\u63a5\\u5c31\\u4e1a\", \"\\u5f85\\u5c31\\u4e1a\"]}, \"link\": {\"source\": [0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 6, 6, 6, 6, 5, 5, 5, 5], \"target\": [5, 6, 5, 6, 5, 6, 5, 6, 5, 6, 7, 8, 9, 10, 7, 8, 9, 10], \"value\": [5, 25, 6, 25, 4, 40, 7, 42, 6, 40, 62, 37, 70, 3, 5, 15, 7, 1]}}], {\"title\": \"\\u5b66\\u9662\\u5b66\\u751f\\u8f6c\\u4e13\\u4e1a\\u7edf\\u8ba1 & \\u6bd5\\u4e1a\\u53bb\\u5411\\u7edf\\u8ba1\", \"font\": {\"size\": 15, \"color\": \"black\"}}, {\"showLink\": true, \"linkText\": \"Export to plot.ly\", \"plotlyServerURL\": \"https://plot.ly\"})});</script><script type=\"text/javascript\">window.addEventListener(\"resize\", function(){window._Plotly.Plots.resize(document.getElementById(\"0bf26a82-2a42-4ab7-b4de-d3dbec0986dd\"));});</script>"
      ],
      "text/vnd.plotly.v1+html": [
       "<div id=\"0bf26a82-2a42-4ab7-b4de-d3dbec0986dd\" style=\"height: 525px; width: 100%;\" class=\"plotly-graph-div\"></div><script type=\"text/javascript\">require([\"plotly\"], function(Plotly) { window.PLOTLYENV=window.PLOTLYENV || {};window.PLOTLYENV.BASE_URL=\"https://plot.ly\";Plotly.newPlot(\"0bf26a82-2a42-4ab7-b4de-d3dbec0986dd\", [{\"type\": \"sankey\", \"node\": {\"pad\": 15, \"thickness\": 15, \"line\": {\"color\": \"black\", \"width\": 0.5}, \"label\": [\"\\u4e13\\u4e1aA\", \"\\u4e13\\u4e1aB\", \"\\u4e13\\u4e1aC\", \"\\u4e13\\u4e1aD\", \"\\u4e13\\u4e1aE\", \"\\u8f6c\\u5230\\u5176\\u4ed6\\u5b66\\u9662\", \"\\u7559\\u5728\\u672c\\u5b66\\u9662\", \"\\u56fd\\u5185\\u5347\\u5b66\", \"\\u56fd\\u5916\\u6df1\\u9020\", \"\\u76f4\\u63a5\\u5c31\\u4e1a\", \"\\u5f85\\u5c31\\u4e1a\"]}, \"link\": {\"source\": [0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 6, 6, 6, 6, 5, 5, 5, 5], \"target\": [5, 6, 5, 6, 5, 6, 5, 6, 5, 6, 7, 8, 9, 10, 7, 8, 9, 10], \"value\": [5, 25, 6, 25, 4, 40, 7, 42, 6, 40, 62, 37, 70, 3, 5, 15, 7, 1]}}], {\"title\": \"\\u5b66\\u9662\\u5b66\\u751f\\u8f6c\\u4e13\\u4e1a\\u7edf\\u8ba1 & \\u6bd5\\u4e1a\\u53bb\\u5411\\u7edf\\u8ba1\", \"font\": {\"size\": 15, \"color\": \"black\"}}, {\"showLink\": true, \"linkText\": \"Export to plot.ly\", \"plotlyServerURL\": \"https://plot.ly\"})});</script><script type=\"text/javascript\">window.addEventListener(\"resize\", function(){window._Plotly.Plots.resize(document.getElementById(\"0bf26a82-2a42-4ab7-b4de-d3dbec0986dd\"));});</script>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "data_trace = dict(\n",
    "    type = 'sankey',\n",
    "    node = dict(\n",
    "      pad = 15,\n",
    "      thickness = 15,\n",
    "      line = dict(color = \"black\", width = 0.5),\n",
    "      label = [\"专业A\", \"专业B\", \"专业C\", \"专业D\", \"专业E\", \"转到其他学院\",\"留在本学院\",\"国内升学\",\"国外深造\",\"直接就业\",\"待就业\"]\n",
    "    ),\n",
    "    link = dict(\n",
    "      source = [0,0,1,1,2,2,3,3,4,4,6,6,6,6,5,5,5,5],\n",
    "      target = [5,6,5,6,5,6,5,6,5,6,7,8,9,10,7,8,9,10],\n",
    "      value = [5,25,6,25,4,40,7,42,6,40,62,37,70,3,5,15,7,1]\n",
    "  )\n",
    ")\n",
    "\n",
    "layout =  dict(\n",
    "    title = \"学院学生转专业统计 & 毕业去向统计\",\n",
    "    font = dict(\n",
    "      size = 15,\n",
    "      color = 'black'\n",
    "    ),\n",
    "    # 修改视图背景色\n",
    "    # plot_bgcolor = 'black',\n",
    "    # paper_bgcolor = 'black'\n",
    ")\n",
    "fig = dict(data=[data_trace], layout=layout)\n",
    "plotly.offline.iplot(fig, validate=False)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
